//入口文件
var express=require("express");
var serveStatic=require('serve-static');
// var mongoose = require("mongoose");
// mongoose.Promise = global.Promise;
var querystring = require("querystring");
var cookieParser = require('cookie-parser');
var session = require('express-session');

var app=express();//创建一个express应用实例
app.set("views","./views/pages");//设置视图根目录
app.set("view engine","pug");//设置默认模板引擎
app.listen(3000);
console.log("nodeWeb started");

app.use(cookieParser("wopelo"));
app.use(session({
	//这里的name指的是cookie的name,默认cookie的name是：connect.sid
	name:"key",
	secret:"wopelo",
	cookie:{maxAge:10*1000},
	//重新保存：强制会话保存即使是未修改的。默认为true但是得写上
	resave: true, 
	//强制“未初始化”的会话保存到存储。 
	saveUninitialized: true,
}));
app.use(serveStatic(__dirname));

function backClient(res, result){
	res.statusCode=200;
	res.setHeader("Content-Type","text/plain");
	// res.setHeader("Access-Control-Allow-Origin","*");
	res.write(JSON.stringify(result));
	res.end();
}

//添加路由
app.get("/",function(req,res){
	if(!req.signedCookies.jack){
		res.cookie("jack","value-content",{maxAge:10*1000,signed:true});
		console.log("添加jack");
	}else{
		console.log(req.signedCookies.jack);
	}

	var sess = req.session;
	if(sess.views){
		sess.views++;
		console.log("sess.views:",sess.views);
	}else{
		sess.views = 1
	}

	//可以证明signed不会作为区分cookie的表示，因为jack在客户端是未被加密的，证明被覆盖
	// res.cookie("jack","value-contenta",{maxAge:10*1000});

	res.render("index",{
		title:"nodeWeb 首页"
	});
});

app.get("/movie/:id",function(req,res){
	if(!req.signedCookies.movie){
		res.cookie("movie","this is a movie",{maxAge:10*1000, signed:true, path:"/"});
		console.log("添加movie");
	}

	res.render("detail",{
		title:"nodeWeb 详情页"
	});
});

app.get("/admin/",function(req,res){
	res.render("admin",{
		title:"nodeWeb 后台管理页"
	});
});

app.get("/admin/movie",function(req,res){
	res.render("movie",{
		title:"nodeWeb 影片管理页"
	});
});

app.get("/admin/list",function(req,res){
	res.render("list",{
		title:"nodeWeb 用户管理页"
	});
});

app.post("/server/findAll",function(req,res){
	let findAll = require("./server/findAll.js");
	findAll().then((result) => {
		backClient(res, result);
	})
});

app.post("/server/target",function(req,res){
	let target = require("./server/target.js");
	let num;
	req.on("data",function(data){
		num = querystring.parse(decodeURIComponent(data)).num
		target(num).then((result) => {
			backClient(res, result);
		})
	});
});

app.post("/server/haveUser",function(req,res){
	let haveUser=require("./server/haveUser.js");
	let name;
	req.on("data",function(data){
		name = querystring.parse(decodeURIComponent(data)).name;
		password = querystring.parse(decodeURIComponent(data)).password;
		haveUser(name,password).then((result) => {
			backClient(res, result);
		});
	});
});

app.post("/server/register",function(req,res){
	let register = require("./server/register.js");
	let name,password;
	req.on("data",function(data){
		name = querystring.parse(decodeURIComponent(data)).name;
		password = querystring.parse(decodeURIComponent(data)).password;
		register(name,password).then((result) => {
			backClient(res, result);
		});
	});
});